﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

namespace WSDatos
{
    public class clsNegocia
    {
        string servidor;
        string bd;
        string password;
        string usuario;
        clsDatos clsInfo = new clsDatos();

        public clsNegocia()
        {            
            servidor = @"localhost";
            bd = "crm";
            usuario = "root";
            password = "root";
            clsInfo.Servidor = servidor;
            clsInfo.Password = password;
            clsInfo.Bd = bd;
            clsInfo.Usuario = usuario;            
        }
        public DataTable FncSelecciona(string comando, string nombre)
        {
            DataTable dtt = new DataTable();
            dtt.TableName = nombre;
            MySqlConnection cnnSql = null;
            try
            {
                cnnSql =(MySqlConnection)clsInfo.Conexion;
                if(cnnSql!=null)
                {
                 if(cnnSql.State== ConnectionState.Open)
                 {
                 dtt = clsInfo.FncTabla(comando,nombre,cnnSql);
                 cnnSql.Close();
                 }
                
                }
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                if (cnnSql != null)
                    if (cnnSql.State == ConnectionState.Open)
                        cnnSql.Close();

            }
            return dtt;
        }
        public DataTable FncSelecciona(MySqlCommand comando, string nombre)
        {
            DataTable dtt = new DataTable();
            dtt.TableName = nombre;
            MySqlConnection cnnSql = null;
            try
            {
                cnnSql = (MySqlConnection)clsInfo.Conexion;
                if (cnnSql != null)
                {
                    if (cnnSql.State == ConnectionState.Open)
                    {
                        MySqlCommand cm01 = (MySqlCommand)comando;
                        cm01.Connection = cnnSql;
                        dtt = clsInfo.FncTabla(comando, nombre);
                        cnnSql.Close();

                    }

                }
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                if (cnnSql != null)
                    if (cnnSql.State == ConnectionState.Open)
                        cnnSql.Close();

            }
            return dtt;
        
        }
        public int FncComando(Object comando)
        {
            int dato = -1;
            MySqlConnection cnnSql = null;
            try
            {
                cnnSql = (MySqlConnection)clsInfo.Conexion;
                if (cnnSql != null)
                {
                    if (cnnSql.State == ConnectionState.Open)
                    {
                        MySqlCommand cm01 = (MySqlCommand)comando;
                        cm01.Connection = cnnSql;
                        dato = clsInfo.FncComando(comando);
                        cnnSql.Close();
                    }

                }

            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                if (cnnSql != null)
                    if (cnnSql.State == ConnectionState.Open)
                        cnnSql.Close();
            
            }
            return dato;
        }
        public Object FncComandoParametros(params Object[] parametros)
        {
            Object parametrosCmd = null;
            
            MySqlCommand cm01 = null;
            cm01 = new MySqlCommand();
            foreach (MySqlParameter par in parametros)
                cm01.Parameters.Add(par);
            parametrosCmd = cm01;
            
            return parametrosCmd;
        }
        public Object FncComandoValores(Object parametrosCmd, params Object[] valores)
        {
            try
            {
                MySqlCommand cm01 = null;
                cm01 = (MySqlCommand)parametrosCmd;
                int cont = 0;
                foreach( Object val in valores)
                    cm01.Parameters[cont++].Value = val;
                parametrosCmd = cm01;
            
            }catch(Exception varEx)
            {
            if(varEx.Message!=""){}
                parametrosCmd= null;

            }
            return parametrosCmd;
        
        }
        public DataTable FncCmdBld(string cmdSel, DataTable dtDatos, string tabla)
        {
            MySqlConnection cnnSql = null;
            try
            {
                cnnSql = (MySqlConnection)clsInfo.Conexion;
                if (cnnSql != null)
                {
                    if (cnnSql.State == ConnectionState.Open)
                    {
                        MySqlDataAdapter daDatos = new MySqlDataAdapter(cmdSel, cnnSql);
                        MySqlCommandBuilder cmdBld = new MySqlCommandBuilder(daDatos);
                        daDatos.Update(dtDatos);
                        cnnSql.Close();
                    }
                }
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                if (cnnSql != null)
                    if (cnnSql.State == ConnectionState.Open)
                        cnnSql.Close();
            }
            return dtDatos;
        }
    }
}
